/**
 * 策略：某一交易日涨停，然后缩量回撤到最低点，单未突破涨停当日的最低价
 */
create or replace function getstnz(days int) returns setof record as
$$
declare
rec record;
begin
  for rec in EXECUTE '
	select b."date",b.code,b.macd, --(b.ma10-b.ma5)/b.ma5 , 
		s."name",s.industry from basic_data b  left join stocks s  on b.code=s.code 
	where 
	to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days||')
		and strpos(s."name",''ST'')=0
		and zd > 9.9 and high=close
		and low <= (select low from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,1) and code=b.code )
	  	and s."timeToMarket" <>0 and to_date(to_char(s."timeToMarket",''99999999''),''YYYYMMDD'') < current_date-300
	  	and --(
			(select low from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,1) and code=b.code ) <=  (select min(low) from basic_data where to_date(date,''YYYY-MM-DD'') >= getwd(current_date,'||days-1||') and code=b.code )
			--OR (select low from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,2) and code=b.code ) <=  (select min(low) from basic_data where to_date(date,''YYYY-MM-DD'') >= getwd(current_date,'||days-1||') and code=b.code )
	--)		
	order by macd::float8
		;
' loop
    return next rec;
  end loop;
return;
end
$$
language 'plpgsql';